﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using Dapper;

namespace GameCell.ConfigDataMaker
{
    public static class DbDataUtil
    {
        private static readonly Dictionary<Type, string> DbTypeMap = new Dictionary<Type, string>()
        {
            {typeof (int), "int"},
            {typeof (long), "bigint"},
            {typeof (string), "text"},
            {typeof (bool), "tinyint(1)"},
            {typeof(float), "float"},
            {typeof(int[]), "text"},
            {typeof(double), "double"},
        };

        public static void CreateStructure<T>(IDbConnection conn)
        {
            Type classType = typeof(T);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"
DROP TABLE IF EXISTS `{0}`;
CREATE TABLE IF NOT EXISTS `{0}` (
", classType.Name);
            //用反射特性来自动完成构建语句
            var properties = classType.GetProperties();
            bool isFirst = true;
            foreach (var propertyInfo in properties)
            {
                string name = propertyInfo.Name;
                string dbtype = DbTypeMap[propertyInfo.PropertyType];
                sb.AppendFormat(@"
`{0}` {1} NOT NULL,", name, dbtype);
            }
            string sql = sb.ToString(0, sb.Length - 1) + ");";
            conn.Execute(sql);
        }

        public static void Save<T>(IDbConnection conn, IEnumerable<T> dataSource)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("insert into `{0}`(", typeof (T).Name);
            bool isFirst = true;
            foreach (var p in typeof (T).GetProperties())
            {
                if (isFirst)
                {
                    isFirst = false;
                }
                else
                {
                    sb.Append(",");
                }
                sb.AppendFormat("`{0}`", p.Name);
            }
            sb.Append(") "); 
            //isFirst = true;
            //foreach (var p in typeof (T).GetProperties())
            //{
            //    if (isFirst)
            //    {
            //        isFirst = false;
            //    }
            //    else
            //    {
            //        sb.Append(",");
            //    }
            //    sb.AppendFormat("@{0}", p.Name);
            //}
            //sb.Append(");");
            //foreach (var data in dataSource)
            //{
            //    conn.Execute(sb.ToString(), data);
            //}

            string insertSqlHeader = sb.ToString();
            foreach (T data in dataSource)
            {
                NameValueCollection nvc = new NameValueCollection();
                StringBuilder sbInsert = new StringBuilder(insertSqlHeader);
                StringBuilder sbUpdate = new StringBuilder();
                sbInsert.Append("Values(");
                isFirst = true;
                foreach (var p in typeof(T).GetProperties())
                {
                    if (isFirst)
                    {
                        isFirst = false;
                    }
                    else
                    {
                        sbInsert.Append(",");
                    }
                    var value = p.GetValue(data);
                    if (p.PropertyType == typeof(bool))
                    {
                        value = String.CompareOrdinal(value.ToString(), "True") == 0 ? "1" : "0";
                    }
                    else if (p.PropertyType == typeof(int[]))
                    {
                        value = string.Join(",", (int[])value);
                    }
                    nvc.Add(p.Name, value.ToString());
                    sbInsert.AppendFormat("'{0}'", value);
                }
                sbInsert.Append(");");
                conn.Execute(sbInsert.ToString());
            }
        }

    }
}
